package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AudioAdapter;
import flc.ast.bean.MyAudioBean;
import flc.ast.databinding.ActivitySelAudioBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.media.MediaLoader;
import stark.common.basic.media.bean.AudioBean;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.ToastUtil;

public class SelAudioActivity extends BaseAc<ActivitySelAudioBinding> {

    private List<MyAudioBean> listShow=new ArrayList<>();
    private AudioAdapter audioAdapter;
    private String selPath;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_audio;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelAudioBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvAudioList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        audioAdapter=new AudioAdapter();
        mDataBinding.rvAudioList.setAdapter(audioAdapter);
        audioAdapter.setOnItemClickListener(this);
        mDataBinding.tvSelAudioImport.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvSelAudioImport:
                if (TextUtils.isEmpty(selPath)){
                    ToastUtils.showShort(getString(R.string.please_select_audio));
                    return;
                }
                AudioFormatActivity.audioPath=selPath;
                startActivity(AudioFormatActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        audioAdapter.getItem(oldPosition).setSelect(false);
        audioAdapter.notifyItemChanged(oldPosition);
        oldPosition=position;
        audioAdapter.getItem(position).setSelect(true);
        audioAdapter.notifyItemChanged(position);
        selPath=audioAdapter.getItem(position).getPath();
    }

    @Override
    protected void initData() {
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_AUDIO)
                .reqPermissionDesc(getString(R.string.get_audio_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }
                })
                .request();
    }

    //获取音频数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<AudioBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<AudioBean>> emitter) {
                List<AudioBean> audioBeans = MediaLoader.loadAudio();
                emitter.onNext(audioBeans);
            }

            @Override
            public void accept(List<AudioBean> selectMediaEntities) {
                listShow.clear();
                if (selectMediaEntities != null) {
                    for (AudioBean bean : selectMediaEntities) {
                        listShow.add(new MyAudioBean(bean.getName(), bean.getPath(), bean.getUri()
                                , ConvertUtils.byte2FitMemorySize(bean.getSize(), 3)
                                , TimeUtils.millis2String(FileUtils.getFileLastModified(bean.getPath()), "yyyy年MM月dd日"), false));
                    }
                }
                if (listShow.size() > 0) {
                    audioAdapter.setList(listShow);
                    mDataBinding.rvAudioList.setVisibility(View.VISIBLE);
                    mDataBinding.llSelAudioNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvAudioList.setVisibility(View.GONE);
                    mDataBinding.llSelAudioNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}